package com.example.j202411.views.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.NumberPicker;
import com.example.j202411.Model.ConstellationResponse;
import com.example.j202411.R;
import com.example.j202411.viewmodel.ConstellationViewModel;
import io.reactivex.rxjava3.annotations.Nullable;
public class ConstellationActivity extends AppCompatActivity {

    private NumberPicker numberPickerConstellation;
    private Button buttonQuery;
    private ConstellationViewModel constellationViewModel;
    private String[] constellations;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_constellation);

        numberPickerConstellation = findViewById(R.id.numberPicker_constellation);
        buttonQuery = findViewById(R.id.button_query);

        constellationViewModel = new ViewModelProvider(this).get(ConstellationViewModel.class);

        // 设置星座选择器的数据
        constellations = getResources().getStringArray(R.array.constellations);
        numberPickerConstellation.setMinValue(0);
        numberPickerConstellation.setMaxValue(constellations.length - 1);
        numberPickerConstellation.setDisplayedValues(constellations);

        // 查询按钮点击事件
        buttonQuery.setOnClickListener(v -> {
            int selectedItem = numberPickerConstellation.getValue();
            if (selectedItem != -1) {
                String selectedConstellation = constellations[selectedItem];
                constellationViewModel.getConstellationInfo(selectedConstellation).observe(this, constellationResponse -> {
                    if (constellationResponse != null) {
                        navigateToDetailActivity(constellationResponse);
                    } else {
                        // Handle error case
                    }
                });
            } else {
                // Handle empty selection
            }
        });
    }

    private void navigateToDetailActivity(ConstellationResponse response) {
        Intent intent = new Intent(this, ConstellationDetailActivity.class);
        intent.putExtra("constellationImage", response.getData().getImage());
        intent.putExtra("constellationInfo", response.getText());
        startActivity(intent);
    }
}